package net.gyapp.markim.module.basic;

import android.text.TextUtils;

import com.alibaba.fastjson.JSONObject;

import net.gyapp.markim.WrapperApplication;
import net.gyapp.markim.module.mine.vo.MemberVo;
import net.gyapp.markim.utils.PreferenceUtils;

import it.sauronsoftware.base64.Base64;


/**
 * Auther:  winds
 * Data:    2018/5/28
 * Version: 1.0
 * Desc:
 */


public class Helper {

    public static final String BASIC_USER = "user"; //用户信息
    public static final String BASIC_TOKEN = "token";
    public static final String BASIC_ACCOUNT = "account";
    public static final String BASIC_CIPHER = "cipher";


    public static void setAccount(String account) {
        PreferenceUtils.putPreference(WrapperApplication.getInstance(), BASIC_ACCOUNT, account);
    }

    public static String getAccount() {
        return PreferenceUtils.getPreference(WrapperApplication.getInstance(), BASIC_ACCOUNT, null);
    }


    public static void setCipher(String cipher) {
        PreferenceUtils.putPreference(WrapperApplication.getInstance(), BASIC_CIPHER, cipher);
    }

    public static String getCipher() {
        return PreferenceUtils.getPreference(WrapperApplication.getInstance(), BASIC_CIPHER, null);
    }



    public final static void saveMemberInfo(MemberVo info) {
        try {
            if (info != null) {
                String result = encode(JSONObject.toJSONString(info));
                PreferenceUtils.putPreference(WrapperApplication.getInstance(), BASIC_USER, result);
            } else {
                setToken(null);
                PreferenceUtils.putPreference(WrapperApplication.getInstance(), BASIC_USER, null);
            }
        } catch (Exception e) {

        }
    }


    public static MemberVo getMemberInfo() {
        String result = PreferenceUtils.getPreference(WrapperApplication.getInstance(), BASIC_USER, null);
        if (!TextUtils.isEmpty(result)) {
            return JSONObject.parseObject(decode(result), MemberVo.class);
        }
        return null;
    }

    /**
     * 清空会员信息 用在退出登陆
     */
    public static void clearMemberInfo() {
//        WrapperApplication.setMember(null);
    }

    public static void setToken(String token) {
        PreferenceUtils.putPreference(WrapperApplication.getInstance(), BASIC_TOKEN, token);
    }

    public static String getToken() {
        return PreferenceUtils.getPreference(WrapperApplication.getInstance(), BASIC_TOKEN, null);
    }

    /**
     * 编码字符串
     *
     * @param data
     * @return
     */
    public static String encode(String data) {
        return data == null ? null : Base64.encode(data);
    }

    /**
     * 解码字符串
     *
     * @param result
     * @return
     */
    public static String decode(String result) {
        return result == null ? null : Base64.decode(result);
    }
}
